package com.tiantian.quanminxiu.activity;

import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jakewharton.rxbinding.view.RxView;
import com.shenglian.utils.utils.CommonUtils;
import com.tiantian.quanminxiu.R;
import com.tiantian.quanminxiu.presenter.RegisterPresent;
import com.tiantian.quanminxiu.utils.APPUtils;
import com.tiantian.quanminxiu.utils.ActivityBase;
import com.tiantian.quanminxiu.view.RegisterView;

import java.util.concurrent.TimeUnit;

import rx.functions.Action1;

/**
 * Created by lzx on 2016/10/24.
 */

public class Activity_Register extends ActivityBase implements RegisterView {
    //region 初始化数据
    private RegisterPresent registerPresent;
    private EditText etLoginUserAccount,etLoginUserPassword,et_password,et_password2;
    private TextView tv_getCode;
    private RelativeLayout vLoginSub;
    private View txtOtherLogin,txtLogin;
    //endregion

    //region onCreate(Bundle savedInstanceState)
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);

        Init();
        InitView();
        registerPresent = new RegisterPresent();
        registerPresent.attachView(this);
    }
    //endregion

    //region InitView()
    @Override
    public void InitView() {
        etLoginUserAccount = (EditText) findViewById(R.id.etLoginUserAccount);
        etLoginUserPassword = (EditText) findViewById(R.id.etLoginUserPassword);
        et_password = (EditText) findViewById(R.id.et_password);
        et_password2 = (EditText) findViewById(R.id.et_password2);
        tv_getCode = (TextView) findViewById(R.id.tv_getCode);
        vLoginSub = (RelativeLayout) findViewById(R.id.vLoginSub);
        txtOtherLogin = findViewById(R.id.txtOtherLogin);
        txtLogin = findViewById(R.id.txtLogin);

        RxView.clicks(tv_getCode).throttleFirst(2000, TimeUnit.MILLISECONDS).map(View->tv_getCode).subscribe(ViewClick);
        RxView.clicks(vLoginSub).map(View->vLoginSub).subscribe(ViewClick);
        RxView.clicks(txtLogin).map(View->txtLogin).subscribe(ViewClick);
        RxView.clicks(txtOtherLogin).map(View->txtOtherLogin).subscribe(ViewClick);
    }
    //endregion

    //region ViewClick
    public Action1<View> ViewClick = v->{
        switch (v.getId()){
            case R.id.txtOtherLogin:
                //跳转选择登陆页面
                APPUtils.LoginSelect_To(thisActivity);
                finish();
                break;
            case R.id.txtLogin:
                //跳转登陆页面
                APPUtils.Login_To(thisActivity);
                finish();
                break;
            case R.id.tv_getCode:
                //获取验证码
                if (etLoginUserAccount.getText().toString().isEmpty()||etLoginUserAccount.getText().toString().length()!=11){
                    CommonUtils.showToask(thisActivity,"请正确输入您的手机号");
                    return;
                }
                registerPresent.getCode(etLoginUserAccount.getText().toString());
                break;
            case R.id.vLoginSub:
                //注册
                if (etLoginUserAccount.getText().toString().isEmpty()||etLoginUserAccount.getText().toString().length()!=11){
                    CommonUtils.showToask(thisActivity,"请正确输入您的手机号");
                    return;
                }
                if (etLoginUserPassword.getText().toString().length()!=5){
                    CommonUtils.showToask(thisActivity,"请正确输入您收到的验证码");
                    return;
                }
                if (et_password.getText().toString().length()<6){
                    CommonUtils.showToask(thisActivity,"请输入不少于6位的密码");
                    return;
                }
                if (!et_password.getText().toString().equals(et_password2.getText().toString())){
                    CommonUtils.showToask(thisActivity,"两次输入密码不一致");
                    return;
                }
                registerPresent.register(etLoginUserAccount.getText().toString(),Integer.valueOf(etLoginUserPassword.getText().toString()),et_password.getText().toString());
                break;
        }
    };
    //endregion

    //region PageInit
    @Override
    public void PageInit() {

    }
    //endregion

    //region onKeyDown(int keyCode, KeyEvent event)
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            //跳转登陆页面
            APPUtils.Login_To(thisActivity);
            finish();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
    //endregion

    //region 得的手机验证码 getPhoneCode(int flag, String msg)
    @Override
    public void getPhoneCode(int flag, String msg) {
        Log.e("flag+msg",msg);
        mHandler.post(task);
        CommonUtils.showToask(thisActivity,msg);
    }
    //endregion

    //region 验证码读秒 mHandler
    private Handler mHandler = new Handler();
    private int intN = 60;
    private final Runnable task = new Runnable() {
        public void run() {
            // 显示
            tv_getCode.setEnabled(false);
            tv_getCode.setText(intN + "秒后重新发送");
            intN--;
            if (intN == 0) {
                tv_getCode.setText("获取验证码");
                tv_getCode.setEnabled(true);
                intN = 60;
            } else {
                mHandler.postDelayed(this, 1000);
            }
        }
    };
    //endregion

    //region 注册结果 register
    @Override
    public void register(int flag, String msg) {
        CommonUtils.showToask(thisActivity,msg);
    }
    //endregion

    //region 调转到主页 JumpMain
    @Override
    public void JumpMain() {
        APPUtils.Main_To(thisActivity);
        finish();
    }
    //endregion

    //region onDestroy
    @Override
    protected void onDestroy() {
        super.onDestroy();
        registerPresent.detachView();
    }
    //endregion
}
